package cn.liweixi.dictionary.controller;

import cn.liweixi.basic.utils.PageList;
import cn.liweixi.basic.utils.Result;
import cn.liweixi.dictionary.domain.DictionaryDetail;
import cn.liweixi.dictionary.query.DictionaryDetailQuery;
import cn.liweixi.dictionary.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "字典明细的API",description="字典相关的功能")
@RequestMapping("/detail")
@RestController
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    //查询所有
    @GetMapping
    public List<DictionaryDetail> findAll(){
        return dictionaryDetailService.findAll();
    }

    //根据id查询数据
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public DictionaryDetail findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return dictionaryDetailService.findById(id);
    }

    //修改或添加
    @PutMapping
    public Result addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail) {
        try {
            if (null == dictionaryDetail.getId()){
                //添加数据
                dictionaryDetailService.add(dictionaryDetail);
            }else{
                //修改数据
                dictionaryDetailService.updateById(dictionaryDetail);
            }
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    //删除数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.delete(id);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    //分页查询
    @PostMapping
    public PageList<DictionaryDetail> queryByPage(@RequestBody DictionaryDetailQuery query){
        return dictionaryDetailService.queryByPage(query);
    }

    //批量删除
    @PatchMapping
    public Result batchDel(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.batchDel(ids);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }
}
